<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">添加承办律师</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<view style="height: 400px;">
				<view class="item-back">
					<uni-forms-item label="承办律师：" labelWidth="100px" required>
						<view class="law-item-back">
							<input @input="onLawyerKeyInput" class="selectLaw" @focus="startLawyerEditAction"
								v-model="code" placeholder="输入律师姓名/手机号/工作组"></input>
							<view v-if="lawyerShowMask" class="selectLaw-list selectLaw-list2">
								<view class="pop-top">
									<view :class="popselect == 1?'pop-top-selected':'pop-top-select'"
										@click="clickPopSelect(1)">选择律师</view>
									<view :class="popselect == 2?'pop-top-selected':'pop-top-select'"
										@click="clickPopSelect(2)">选择工作组</view>
								</view>
								<scroll-view class="pop-content-back" scroll-y="true">
									<view class="pop-content-list" v-if="popselect == 1"
										v-for="(item,index) in lawyerArr" @click="clickLawyerPopIremAction(item)">
										<image class="pop-content-img" :src="item.account.avatarPic"></image>
										<view class="pop-content-name">{{item.account.realName}}</view>
										<view class="pop-content-phone">{{item.account.mobile}}</view>
									</view>
									<view class="pop-content-list" v-if="popselect == 2"
										v-for="(item,index) in teamList" @click="clickPopTeamAction(item)">
										<image class="pop-content-img" src="../../../static/pc_customer.png"></image>
										<view class="pop-content-name">{{item.name}}</view>
										<view class="pop-content-phone pop-content-user"><text
												v-for="(value,index) in item.users">{{value.account.realName}}<text
													v-if="index < item.users.length - 1">,</text></text></view>
									</view>
								</scroll-view>
							</view>
						</view>
						<view class="mask-view" v-if="lawyerShowMask" @tap="tapLawyerMask"></view>
						<view class="customer-item-back">
							<view class="customer-item" v-for="(item,index) in lawyerSelectedArr">
								<view class="customer-back">
									<image class="customer-img" :src="item.account.avatarPic"></image>
									<view class="customer-name">
										{{item.account.realName?item.account.realName:item.account.nickName}}
									</view>
									<view class="customer-phone">{{item.account.mobile}}</view>
									<image class="customer-close" @click="clickLawyerDelAction(index)"
										src="../../../static/phone/guanbi1.png"></image>
								</view>
							</view>
						</view>
					</uni-forms-item>
				</view>
			</view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="saveAction">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	let app = ''

	export default {
		props: {
			hasLawyers: { //已选择律师
				type: Array,
				default: []
			},
			selectItemID: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				role: '', //当前登录角色
				roleUrl: '',
				code: '',
				lawyer: '',
				lawyerList: [], //常用律师
				lawyerArr: [], //常用律师去掉已选
				lawyerSelectedArr: [], //常用律师已选择
				lawyerShowMask: false, //常用律师

				popselect: 1,
				teamList: [], //工作组
				teamArr: [], //工作组去掉已选
				teamSelectedArr: [], //工作组已选择
			}
		},
		mounted() {
			var _this = this;
			app = getApp()
			_this.role = app.globalData.role
			_this.roleUrl = _this.$roleUrl({
				tab: _this.role
			})
			this.requestLawyerData();
			this.requestTeamData();
		},
		methods: {

			//切换工作组、律师
			clickPopSelect(index) {
				this.popselect = index;
			},
			//常用律师：网络请求
			async requestLawyerData() {
				let _this = this
				var url = `/${_this.roleUrl}/lawyer/selector`;
				let parmas = {};
				if (_this.code) {
					this.$set(parmas, 'keyword', _this.code)
				}
				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {

					_this.lawyerList = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var value = res.data.data[i];
						var hasSelect = false;
						for (var j = 0; j < _this.hasLawyers.length; j++) {
							var tem = _this.hasLawyers[j];
							if (value.account.id == tem.accId) {
								hasSelect = true;
							}
						}
						if (!hasSelect) {
							_this.lawyerList.push(value);
						}
					}
					_this.refreshLawyerDataAction();
				}
			},

			//常用律师：开始输入
			startLawyerEditAction(e) {
				console.log(e)
				this.lawyerShowMask = true
			},
			//常用律师：输入
			onLawyerKeyInput(event) {
				console.log(event.target.value)
				this.requestLawyerData();
				this.requestTeamData();
			},
			//常用律师：隐藏
			tapLawyerMask() {
				this.lawyerShowMask = false
			},
			//常用律师：选择
			clickLawyerPopIremAction(item) {
				this.lawyerShowMask = false;
				this.lawyerSelectedArr.push(item);
				this.refreshLawyerDataAction();
			},
			//工作组：选择
			clickPopTeamAction(item) {
				this.lawyerShowMask = false;
				for (var i = 0; i < item.users.length; i++) {
					var value = item.users[i];
					var hasnot = true;
					for (var j = 0; j < this.lawyerSelectedArr.length; j++) {
						var ele = this.lawyerSelectedArr[j];
						if (value.accId == ele.accId) {
							hasnot = false;
						}
					}

					for (var j = 0; j < this.hasLawyers.length; j++) {
						var ele = this.hasLawyers[j];
						if (value.accId == ele.accId) {
							hasnot = false;
						}
					}
					if (hasnot) {
						this.lawyerSelectedArr.push(value);
					}
				}
				this.refreshLawyerDataAction();
			},

			//常用律师：刷新列表
			refreshLawyerDataAction() {
				this.lawyerArr = [];
				for (var i = 0; i < this.lawyerList.length; i++) {
					var value = this.lawyerList[i];
					var hasSelect = false;
					for (var j = 0; j < this.lawyerSelectedArr.length; j++) {
						var tem = this.lawyerSelectedArr[j];
						if (value.account.id == tem.account.id) {
							hasSelect = true;
						}
					}
					if (!hasSelect) {
						this.lawyerArr.push(value);
					}
				}
			},
			//常用律师：删除已选用户
			clickLawyerDelAction(index) {
				this.lawyerSelectedArr.splice(index, 1);
				this.refreshLawyerDataAction();
			},

			//常用工作组：网络请求
			async requestTeamData() {
				let _this = this
				var url = '/admin/workGroup/list';
				let parmas = {};
				if (_this.code) {
					this.$set(parmas, 'name', 'lk:' + _this.code)
				}
				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.teamList = res.data.data.list;
					_this.refreshLawyerDataAction();
				}
			},

			async saveAction() {
				if (this.lawyerSelectedArr.length == 0) {
					uni.showToast({
						title: '请添加律师',
						icon: 'none'
					})
					return;
				}
				let _this = this

				var arr = [];
				for (var i = 0; i < this.lawyerSelectedArr.length; i++) {
					var item = this.lawyerSelectedArr[i];
					
					arr.push(item.id)
				}
				var parms = {
					'caseId': _this.selectItemID,
					'lawyerIds': arr,
				}
				console.log(parms)

				var url = "/admin/caseUser/lawyers/0";
				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parms
				})
				console.log('返回结果', res)

				if (res.data.code == 200) {
					uni.showToast({
						title: '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				}
			},


			closeBtnAction() {
				this.$emit('lawyerAddClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 199;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 210;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 552px;
		height: 500px;
		padding: 24px;
		max-height: 540px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 211;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;

	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {

		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.item-back {
		display: flex;
		/* 		align-items: center;
 */
	}

	.item-name {
		width: 80px;
		height: 20px;
		font-size: 14px;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.law-item-view-back {
		display: flex;
		flex-direction: column;
	}

	.law-item-back {
		background-color: white;
		width: 400px;
	}

	.selectLaw {
		font-size: 14px;
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		padding: 6px 10px;
		position: relative;

		flex-direction: row;
		align-items: center;
		z-index: 100000;
		background-color: white;
	}

	.selectLaw-list {
		margin-top: 2px;
		position: absolute;
		width: 400px;
		height: 270px;
		z-index: 100010;
		background-color: white;
		box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2000);
		border-radius: 2px;
	}

	.pop-top {
		display: flex;
		margin-left: 20px;

		width: 258px;
		border-bottom: 1px solid #EEEEEE;
	}

	.pop-top-selected {
		width: 85px;
		height: 38px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 12px;
		font-weight: 400;
		color: #2DA9C5;
		border-bottom: #2DA9C5 1px solid;
	}

	.pop-top-select {
		width: 85px;
		height: 38px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 12px;
		font-weight: 400;
		color: #999999;
	}


	.pop-content-back {
		margin-left: 20px;
		width: 378px;
		height: 230px;
	}

	.pop-content-list {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
		margin-top: 10px;
	}

	.pop-content-img {
		width: 20px;
		min-width: 20px;
		height: 20px;
		border-radius: 50%;
	}

	.pop-content-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 6px;
	}

	.pop-content-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 10px;
	}

	.customer-item {
		display: flex;
		margin-top: 8px;
		align-items: center;
		margin-right: 20px;
	}

	.customer-back {
		/* min-width: 168px; */
		width: 190px;
		height: 44px;
		background: #E8F7FB;
		border-radius: 4px;
		display: flex;
		align-items: center;
	}

	.customer-img {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		margin-left: 8px;
	}

	.customer-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 2px;
	}

	.customer-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 4px;
	}

	.customer-close {
		width: 16px;
		height: 16px;
		margin-right: 2px;
		margin-left: auto;
	}

	.mask-view {
		position: absolute;
		width: 100vw;
		height: 100vh;
		z-index: 9999;
		background-color: rgba(0, 0, 0, 0.0);
		top: -100px;
		left: 0;
		right: 0;
		bottom: 0;
	}

	.customer-item-back {
		width: 480px;
		display: flex;
		flex-wrap: wrap;
	}
</style>
